package com.china08.yxyapi.api.manage.sch.office.meetingv1_1;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.meeting.VideoMeeting;
import com.china08.yxyapi.entity.db.meeting.VideoMeetingFile;
import com.china08.yxyapi.entity.db.meeting.VideoMeetingUser;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.VideoMeetingFileRepository;
import com.china08.yxyapi.repository.db.VideoMeetingRepository;
import com.china08.yxyapi.repository.db.VideoMeetingUserRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.service.baijiayun.impl.MeetingRoomServiceImpl;
import com.china08.yxyapi.util.DateUtils;

/**
 * 创建视频会议
 *
 */
@RestController("sch_meeting_save")
public class VideoMeetingv1_1SaveController extends YxyApiBaseController {

    @Autowired
    private VideoMeetingRepository videoMeetingRepository;

    @Autowired
    private VideoMeetingFileRepository videoMeetingFileRepository;

    @Autowired
    private MeetingRoomServiceImpl meetingRoomServiceImpl;

    @Autowired
    private VideoMeetingUserRepository videoMeetingUserRepository;

    @RequestMapping(value = "/api_yw/manage/sch/office/meetingv1_1/videomeet/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel)
            throws ParseException {

        reqModel.check();

        RespModel model = new RespModel();
        VideoMeeting vMeeting = new VideoMeeting();
        String date = DateUtils.formatDate2(reqModel.date);
        String startDate = date + " " + reqModel.startDate + ":00";
        String endDate = date + " " + reqModel.endDate + ":00";
        Date start = DateUtils.formatStrDate(startDate);
        Assert.isTrue(DateUtils.diffmin(start, new Date()) == 1, "开始时间不能小于当前时间");
        Date end = DateUtils.formatStrDate(endDate);
        Assert.isTrue(DateUtils.diffmin(end, new Date()) == 1, "结束时间不能小于当前时间");
        Assert.isTrue(DateUtils.diffmin(end, start) == 1, "开始时间不能大于等于结束时间");

        YxyProfile profile = getProfile(request, response);
        String schoolNick = profile.getSchoolName();
        String schoolId = profile.getSchoolId();
        String createById = profile.getId();
        String createByName = profile.getUserNick();

        vMeeting.setCreatedBy(createById);
        vMeeting.setCreatedByName(createByName);
        vMeeting.setSchoolId(schoolId);
        vMeeting.setSchoolName(schoolNick);
        vMeeting.setTitle(reqModel.title);
        vMeeting.setStartDate(start);
        vMeeting.setEndDate(end);
        vMeeting.setSpeakerId(reqModel.userId);
        vMeeting.setSpeakerName(reqModel.userNick);
        vMeeting.setSpeakerMobile(reqModel.mobile);
        vMeeting.setIntro(reqModel.intro);
        if (StringUtils.isNotBlank(reqModel.meetingLabel)) {// v1.1
            vMeeting.setMeetingLabel(reqModel.meetingLabel);
        }
        vMeeting.setStatus(0);
        vMeeting = videoMeetingRepository.save(vMeeting);
        meetingRoomServiceImpl.meetingRoomCrt(vMeeting.getId());

        vMeeting = videoMeetingRepository.findById(vMeeting.getId());
        Assert.notNull(vMeeting, "发起会议失败");

        if (StringUtils.isNoneBlank(reqModel.fileUrl) && StringUtils.isNoneBlank(reqModel.filesName)) {
            VideoMeetingFile file = new VideoMeetingFile();
            file.setMeetingId(vMeeting.getId());
            file.setAttachment(reqModel.fileUrl);
            file.setAttachmentName(reqModel.filesName);
            videoMeetingFileRepository.save(file);
        }

        // v1.1
        List<VideoMeetingUser> meetingUserLst = new ArrayList<VideoMeetingUser>();
        for (MeetingUser user : reqModel.meetingUsersLst) {
            VideoMeetingUser meetingUser = new VideoMeetingUser();
            meetingUser.setMeetingId(vMeeting.getId());
            meetingUser.setUserId(user.id);
            meetingUser.setUserNick(user.name);
            meetingUser.setUserType(user.type);
            meetingUserLst.add(meetingUser);
        }
        videoMeetingUserRepository.save(meetingUserLst);

        model.setMessage("发起会议成功");
        return model;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiPageReqModel {
        // 会议主题
        public String title;
        // 开始日期
        public Date date;
        // 开始时间
        public String startDate;
        // 结束时间
        public String endDate;
        // 主讲人id
        public String userId;
        // 主讲人名称
        public String userNick;
        // 手机号
        public String mobile;
        // 会议内容
        public String intro;

        // 附件url
        public String fileUrl;
        // 附件名称
        public String filesName;

        // 会议类型
        public String meetingLabel;// v1.1

        // 参会人列表
        public List<MeetingUser> meetingUsersLst;// v1.1

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(title, null), "请输入1-36个字的会议主题");
            Assert.isTrue(title.length() <= 36, "请输入1-36个字的会议主题");
            Assert.notNull(date, "请选择会议时间");
            Assert.notNull(StringUtils.defaultIfBlank(startDate, null), "请选择会议开始时间");
            Assert.notNull(StringUtils.defaultIfBlank(endDate, null), "请选择会议结束时间");
            Assert.notNull(StringUtils.defaultIfBlank(userId, null), "请选择主讲人");
            Assert.notNull(StringUtils.defaultIfBlank(userNick, null), "请选择主讲人");
            Assert.notNull(StringUtils.defaultIfBlank(mobile, null), "请选择主讲人");
            Assert.notNull(StringUtils.defaultIfBlank(intro, null), "请输入1-8000个字的会议内容");
            Assert.isTrue(title.length() <= 8000, "请输入1-8000个字的会议内容");
            Assert.isTrue(meetingUsersLst != null && meetingUsersLst.size() > 0, "参会人员不能为空");// v1.1
            Assert.isTrue(meetingLabel.length() <= 10, "自定义类型字数范围1-10个字");// v1.1
        }
    }

    protected static class MeetingUser {
        // 参与人、学校、省、市id
        public String id;
        // 参与人、学校、省、市name
        public String name;
        // 类型 0:个人, 1-学校, 2-区县, 3-市.
        public String type;
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {

    }
}
